﻿#include "FITKAbaqusHDF5AdaptorSectionAssign.h"
#include "FITK_Kernel/FITKCore/FITKEnumTransformer.hpp"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Reader.h"
#include "FITK_Interface/FITKInterfaceIO/FITKAbstractHDF5Writer.h"

#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractMaterial.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKAbstractSection.h"
#include "FITK_Interface/FITKInterfacePhysics/FITKSectionAssign.h"

#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignSolidHomogenous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignShellHomogeneous.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignBeam.h"
#include "FITK_Interface/FITKInterfaceStructural/FITKAbaSectionAssignShell.h"

namespace IO
{
    QString FITKAbaqusHDF5AdaptorSectionAssign::getAdaptorClass()
    {
        return "FITKAbaqusHDF5AdaptorSectionAssgin";
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::adaptR()
    {
        auto sectionAssign = dynamic_cast<Interface::FITKSectionAssign*>(_dataObj);
        if (!_reader || !_h5Group || !sectionAssign) return false;
        bool isR = true;
        isR &= readSectionAssign(sectionAssign, *_h5Group);
        return isR;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::adaptW()
    {
        auto sectionAssign = dynamic_cast<Interface::FITKSectionAssign*>(_dataObj);
        if (!_writer || !_h5Group || !sectionAssign) return false;
        bool isW = true;
        isW &= writeSectionAssign(sectionAssign, *_h5Group);
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::readSectionAssign(Interface::FITKSectionAssign * section, H5::Group & h5Group)
    {
        Interface::FITKAbaAbastractSectionAssign* sectionAssign = dynamic_cast<Interface::FITKAbaAbastractSectionAssign*>(section);
        if (!sectionAssign || h5Group.getLocId() == -1) return false;
        bool isR = true;

        isR &= FITKInterfaceHDF5AdaptorSectionAssign::readSectionAssign(sectionAssign, h5Group);

        auto type = sectionAssign->getSectionAssignType();
        if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::Beam)
        {
            isR &= readSectionAssignBeam(sectionAssign, h5Group);
        }
        else if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::ShellHomogeneous)
        {
            isR &= readSectionAssignShellHomogenous(sectionAssign, h5Group);
        }
        else if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::SolidHomogeneous)
        {
            isR &= readSectionAssignSolidHomogenous(sectionAssign, h5Group);
        }
        else if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::Truss)
        {
            isR &= false;
        }

        return isR;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::readSectionAssignBeam(Interface::FITKSectionAssign* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionAssignBeam* sectionBeam = dynamic_cast<Interface::FITKAbaSectionAssignBeam*>(section);
        if (!sectionBeam) return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::readSectionAssignSolidHomogenous(Interface::FITKSectionAssign* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionAssignSolidHomogenous* sectionSolid = dynamic_cast<Interface::FITKAbaSectionAssignSolidHomogenous*>(section);
        if (!sectionSolid) return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::readSectionAssignShellHomogenous(Interface::FITKSectionAssign* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionAssignShellHomogeneous* sectionShell = dynamic_cast<Interface::FITKAbaSectionAssignShellHomogeneous*>(section);
        if (!sectionShell)return false;
        bool isValid = false;

        auto strShellOffset = readStrAttribute(h5Group, "ShellOffset");
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKShellAssignEnum::ShellOffset> fitkShellTypeTrafer;
        auto eShellOffset = fitkShellTypeTrafer.fromString(QString::fromStdString(strShellOffset), isValid);
        if (!isValid) return false;
        sectionShell->setShellOffset(eShellOffset);

        auto ThicknessAssignment = readStrAttribute(h5Group, "ThicknessAssignment");
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKShellAssignEnum::ShellThicknessAssignment> fitkTATypeTrafer;
        auto eThicknessAssignment = fitkTATypeTrafer.fromString(QString::fromStdString(ThicknessAssignment), isValid);
        if (!isValid) return false;
        sectionShell->setThicknessAssignment(eThicknessAssignment);

        double ShellOffsetValue = readDoubleAttribute(h5Group, "ShellOffsetValue");
        sectionShell->setShellOffsetValue(ShellOffsetValue);

        return true;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::writeSectionAssign(Interface::FITKSectionAssign * section, H5::Group & h5Group)
    {
        Interface::FITKAbaAbastractSectionAssign* sectionAssign = dynamic_cast<Interface::FITKAbaAbastractSectionAssign*>(section);
        if (!sectionAssign || h5Group.getLocId() == -1) return false;
        bool isW = true;
        bool isValid = false;

        isW &= FITKInterfaceHDF5AdaptorSectionAssign::writeSectionAssign(sectionAssign, h5Group);

        auto type = sectionAssign->getSectionAssignType();
        if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::Beam)
        {
            isW &= writeSectionAssignBeam(sectionAssign, _h5CreateGroup);
        }
        else if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::ShellHomogeneous)
        {
            isW &= writeSectionAssignShellHomogenous(sectionAssign, _h5CreateGroup);
        }
        else if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::SolidHomogeneous)
        {
            isW &= writeSectionAssignSolidHomogenous(sectionAssign, _h5CreateGroup);
        }
        else if (type == Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType::Truss)
        {
            isW &= false;
        }

        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKAbaAbastractSectionAssign::FITKAbaSectionAssignType> fitkITypeTrafer;
        auto SectionAssignType = fitkITypeTrafer.toStrting(type, isValid);
        if (!isValid) return false;
        writeStrAttribute(_h5CreateGroup, "Type", SectionAssignType.toStdString());
        return isW;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::writeSectionAssignBeam(Interface::FITKSectionAssign* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionAssignBeam* sectionBeam = dynamic_cast<Interface::FITKAbaSectionAssignBeam*>(section);
        if (!sectionBeam) return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::writeSectionAssignSolidHomogenous(Interface::FITKSectionAssign* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionAssignSolidHomogenous* sectionSolid = dynamic_cast<Interface::FITKAbaSectionAssignSolidHomogenous*>(section);
        if (!sectionSolid) return false;
        return true;
    }

    bool FITKAbaqusHDF5AdaptorSectionAssign::writeSectionAssignShellHomogenous(Interface::FITKSectionAssign* section, H5::Group& h5Group)
    {
        Interface::FITKAbaSectionAssignShellHomogeneous* sectionShell = dynamic_cast<Interface::FITKAbaSectionAssignShellHomogeneous*>(section);
        if (!sectionShell)return false;
        bool isValid = false;

        auto eShellOffset = sectionShell->getShellOffset();
        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKShellAssignEnum::ShellOffset> fitkShellTypeTrafer;
        QString ShellOffsetStr = fitkShellTypeTrafer.toStrting(eShellOffset, isValid);
        if (!isValid) return false;
        writeStrAttribute(h5Group, "ShellOffset", ShellOffsetStr.toStdString());

        //枚举转换字符
        Core::FITKEnumTransfer<Interface::FITKShellAssignEnum::ShellThicknessAssignment> fitkThTypeTrafer;
        QString ThicknessAssignmentStr = fitkThTypeTrafer.toStrting(sectionShell->getThicknessAssignment(), isValid);
        if (!isValid) return false;
        writeStrAttribute(h5Group, "ThicknessAssignment", ThicknessAssignmentStr.toStdString());

        double ShellOffsetValue = sectionShell->getShellOffsetValue();
        writeDoubleAttribute(h5Group, "ShellOffsetValue", &ShellOffsetValue);

        return true;
    }
}